<?php

abstract class Database
{

    protected $conn;

    protected $result;

    protected $dbname;

    function __construct($name)
    {
        $this->dbname = $name;
        $this->conn = getConnection();
    }

    /**
     * 直接执行一个SQL指令
     *
     * @param string $sql
     * @return NULL|mysqli_result|boolean 返回结果
     */
    public function query($sql)
    {
        
        if ($this->result) {
            $this->result = NULL;
        }
        $this->result = $this->conn->query($sql);
        addQueryTotal();
        if (! $this->result) {
            debug();
            printf("Error: %s\n", $this->conn->error);
            exit();
        }
        return $this->result;
    }

    /**
     * 通过UID获取一切
     *
     * @param string $uid
     * @return array
     */
    public function fetch_all_by_uid($uid)
    {
        return $this->fetch_assoc("`uid`='$uid'");
    }

    /**
     * 返回结果集合
     *
     * @return NULL|mysqli_result|boolean
     */
    public function getResult()
    {
        return $this->result;
    }

    /**
     * 以assoc形式返回数据库查询结果
     * 如果$where为空则默认查询缓存中的结果集
     * 自动拼接语句格式:
     * SELECT $select FROM `dbname` WHERE $where
     *
     * @param string $where
     * @param string $select
     * @return array
     */
    public function fetch_assoc($where = "", $select = "*")
    {
        if ($where == "") {
            if ($this->result == NULL) {
                throw "You must query first!";
            }
        } else {
            $this->query("SELECT $select FROM `" . $this->dbname . "` WHERE $where");
            return $this->result->fetch_assoc();
        }
        return $this->result->fetch_assoc();
    }

    public function fetch_all_assoc($where, $select = "*", $order = "")
    {
        if (! $order)
            $this->query("SELECT $select FROM `" . $this->dbname . "` WHERE $where");
        else {
            $this->query("SELECT $select FROM `" . $this->dbname . "` WHERE $where " . $order);
        }
        if (! $this->result->num_rows) {
            return array();
        }
        $res = array();
        $i = 0;
        while ($i <= $this->result->num_rows - 1) {
            $i ++;
            $res[$i] = $this->result->fetch_assoc();
        }
        return $res;
    }

    /**
     * 以fields形式返回数据库查询结果
     * 如果$where为空则默认查询缓存中的结果集
     * 自动拼接语句格式:
     * SELECT $select FROM `dbname` WHERE $where
     *
     * @param string $where
     * @param string $select
     * @return array
     */
    function fetch_all($where = "", $select = "*")
    {
        if ($where == "") {
            if ($this->result == NULL) {
                throw "You must query first!";
            }
        } else {
            $this->query("SELECT $select FROM `" . $this->dbname . "` WHERE $where");

            return $this->result->fetch_fields();
        }
        return $this->result->fetch_fields();
    }

    function insert($contant = array(), $ignore = array())
    {
        
        $key = "(`";
        $values = "(";
        $num = count($contant);
        if (count($contant) == 0) {
            return false;
        }
        $now = 0;
        foreach ($contant as $k => $v) {
            $now ++;
            $key = $key . $k . "`";
            if (in_array($k, $ignore))
                $values = $values . $v;
            else
                $values = $values . "'" . $v . "'";
            if ($now == $num) {
                $key = $key . ")";
                $$values = $values . ")";
            } else {
                $key = $key . ", `";
                $values = $values . ", ";
            }
        }
        return $this->query("INSERT INTO `" . $this->dbname . "` $key VALUES $values)");
    }

    function update($contant = array(), $where, $ignore = array())
    {
        if (count($contant) == 0) {
            return false;
        }
        $sql = "UPDATE `" . $this->dbname . "` SET ";
        $now = 0;
        $num = count($contant);
        foreach ($contant as $k => $v) {
            $now ++;
            if (in_array($k, $ignore))
                $sql = $sql . "`$k` = $v";
            else
                $sql = $sql . "`$k` = '$v'";
            if ($now != $num)
                $sql = $sql . ", ";
        }
        $sql = $sql . " WHERE $where";
        return query($sql);
    }

    /**
     * 查询返回结果是否为空
     * * @return boolean
     */
    public function isEmpty()
    {
        if ($this->result == NULL) {
            throw "You must query first!";
        }
        return $this->result->num_rows == 0;
    }

    public function getRows()
    {
        if ($this->result == NULL) {
            throw "You must query first!";
        }
        return $this->result->num_rows;
    }

    public function getUniqueID()
    {
        return mysqli_insert_id($this->conn);
    }

    function remove($where)
    {
        if ($where == "") {
            return false;
        }
        return $this->query("DELETE FROM `" . $this->dbname . "` WHERE $where");
    }

    public function count_all()
    {
        $this->query("SELECT COUNT(*) FROM " . $this->dbname);
        return $this->result->fetch_assoc()["COUNT(*)"];
    }

    public function count_something($where)
    {
        $this->query("SELECT COUNT(*) FROM " . $this->dbname . " WHERE $where");
        return $this->result->fetch_assoc()["COUNT(*)"];
    }

    public function fetch_assoc_by_sql($sql)
    {
        $this->query($sql);
        return $this->result->fetch_assoc();
    }

    public function fetch_all_assoc_by_sql($sql)
    {
        $this->query($sql);
        if (! $this->result->num_rows) {
            return array();
        }
        $res = array();
        $i = 0;
        while ($i <= $this->result->num_rows - 1) {
            $i ++;
            $res[$i] = $this->result->fetch_assoc();
        }
        return $res;
    }
}
?>